#include "stdafx.h"

extern WSASetLastError_proxy_t pTrueWSASetLastError;

int ioctlsocket_proxy ( 
    __in     SOCKET  s,
    __in     long    cmd,
    __inout  u_long* argp
    )
{
    try
    {
    //SEND
        Header header(GetCurrentThreadId(), "ioctlsocket");
        ioctlsocket_in_inout ioctlsocketIN(s, cmd, argp);

        std::vector<char> out_buffer;
        Serialize(header, ioctlsocketIN, out_buffer);

        SendToClient (out_buffer);

    //RECEIVE()
        ioctlsocket_out_inout ioctlsocketOUT;

        std::vector<char> in_buffer;
        
        RecvFromClient (in_buffer, header);
        Deserialize deserialize(in_buffer);
        deserialize(ioctlsocketOUT);

        if(ioctlsocketOUT.wsa_last_error_ != 0) 
        {
            pTrueWSASetLastError(ioctlsocketOUT.wsa_last_error_);
            return SOCKET_ERROR;
        }
        else if (argp)
        {
            *argp = *ioctlsocketOUT.argp_;
        }
        return 0;
    }
    catch (std::exception& ex)
    {
        LOG(plog::error) << "ioctlsocket: " << ex.what();
    }
    pTrueWSASetLastError (WSAEWOULDBLOCK);
    return SOCKET_ERROR;
}
